"""
Copyright 2021-2021 The jdh99 Authors. All rights reserved.
ssd1306操作封装.支持多种英文字库
Authors: 鬼谷邪玉 <admin@0chem.com>
"""

from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
import sys, os


# print(sys.version)

class OLED:
    # 初始化i2c接口
    # param scl: i2c的时钟脚
    # param sda: i2c的数据脚
    # param width: oled屏幕的宽度像素
    # param height: oled屏幕的高度像素
    # param i2c: i2c口
    def __init__(self, scl, sda, width, height, i2c=-1):
        # self._i2c = I2C(i2c, scl=Pin(scl), sda=Pin(sda))
        self._i2c = SoftI2C(scl=Pin(scl), sda=Pin(sda))
        self._width = width
        self._height = height
        self._oled = SSD1306_I2C(self._width, self._height, self._i2c)

    # 清除屏幕
    def clear(self):
        self._oled.fill(0)

    # 屏幕刷新显示
    def show(self):
        self._oled.show()

    # 画点
    def pixel(self, x, y, v):
        if x < self._width and y < self._height:
            self._oled.pixel(x, y, v)

    # 显示字符串.注意字符串必须是英文或者数字
    def text(self, string, x_axis, y_axis):
        self._oled.text(string, x_axis, y_axis)

    # 用字体变量，显示字符串.注意字符串必须是英文或者数字，不建议加载中文
    def text_withfont(self, string, x_axis, y_axis, font, font_size):
        offset = 0
        for k in string:
            data_code = k.encode("utf-8")
            byte_data = len(data_code)
            if byte_data == 1:
                byte_data = font[int(data_code[0])]
            elif byte_data == 3:
                code = 0x00 # 将中文转成16进制编码
                code |= data_code[0] << 16
                code |= data_code[1] << 8
                code |= data_code[2]
                return
                byte_data = font[code]
            for y in range(0, font_size):
                a = bin(int(byte_data[y])).replace('0b', '')
                while len(a) < 8:
                    a = '0' + a
                for x in range(0, 8):
                    # self._oled.pixel(x_axis + offset + x, y_axis+y, int(a[x]))
                    self.pixel(x_axis + offset + x, y_axis + y, int(a[x]))  # 修改后防止出边
            offset += 8

    # 这个会将文件加载成变量，注意文件大小，过大会导致死机
    def load_font(self, font_path):
        font = []
        with open(font_path, 'r') as f:
            for l in f.readlines():
                font.append(l.split(','))
        return font
    
    # 显示字符串.注意字符串必须是英文或者数字，不建议加载中文
    def text_with_fontfile(self, string, x_axis, y_axis, font_name, font_size,font_rowcount):
        offset = 0
        for k in string:
            data_code = k.encode("utf-8")
            byte_data = len(data_code)
            if byte_data == 1:
                byte_data = font[int(data_code[0])]
            elif byte_data == 3:
                code = 0x00 # 将中文转成16进制编码
                code |= data_code[0] << 16
                code |= data_code[1] << 8
                code |= data_code[2]
                return
                byte_data = font[code]
            for y in range(0, font_size):
                a = bin(int(byte_data[y])).replace('0b', '')
                while len(a) < 8:
                    a = '0' + a
                for x in range(0, 8):
                    # self._oled.pixel(x_axis + offset + x, y_axis+y, int(a[x]))
                    self.pixel(x_axis + offset + x, y_axis + y, int(a[x]))  # 修改后防止出边
            offset += 8


o = OLED(32, 33, 128, 64)
o.text('wendu', 0, 0)
o.text('name title', 0, 20)
font = o.load_font('/oled/ascii16.txt')
o.text_withfont("0123456789abcdefghigklmnopkrstuvwxyz", 0, 0, font, 16)  # 0123456789abcdefghigklmnopkrstuvwxyz中国汉语
o.show() # 必须添加，否则不显示，就是绘图的最后执行命令
print('END')
pass  # END
